<?php
namespace Home\Controller;
use Think\Controller;
class ShuadanController extends ParentController {
	public function _initialize(){
		parent::_initialize();
	}
    public function index(){
    	// 查询最近一次的计划
    	$last_plane = M('plane')->order('id desc')->find();
    	$plane_execute = M('shuadan')->where("plane_id=%d",$last_plane['id'])->select();

        $this->assign('dans',$plane_execute);
    	$this->assign('plane',$last_plane);
        $this->display();
    }
    // 查看某周期详情
    public function show($id){
        $last_plane = M('plane')->where("id='%d'",$id)->find();
        $plane_execute = M('shuadan')->where("plane_id=%d",$last_plane['id'])->select();

        $this->assign('dans',$plane_execute);
        $this->assign('plane',$last_plane);
        $this->display();
    }
    // 添加计划
    public function add(){
    	$this ->display();
    }
    // 修改实际刷单
    public function edit($id){
        $data = M('shuadan')->where("id='%d'",$id)->find();
        $this->assign('data',$data);
        $this->display();
    }
    public function update($id){
        if(IS_POST){
            $dan = M('shuadan')->where("id='%d'",$id)->find();
            $data['real_num'] = (int)I('real_num');
            $data['real_flow'] = (int)I('real_flow');
            $data['description'] = I('description');
            $data['num_contrast'] = $data['real_num'] - $dan['plane_num'];
            $data['real_conver'] = round((float)($data['real_num']/$data['real_flow']),5);
            $data['conver_contrast'] = round($data['real_conver'] - $dan['plane_conver'],5);

            // 更新数据库
            $result = M('shuadan')->where("id='%d'",$id)->save($data);
            $this->success('更新成功！','/Home/Shuadan/index');
        }else{
            $this->error('404 NOT FOUND!');
        }
        
    }
    // 添加计划
    public function store(){
    	// 记录计划内容
        if(IS_POST){
        	$input['plane_name'] = I('plane_name');
        	$input['start_time'] = strtotime(I('start_time'));
        	$input['days'] = I('days');
        	$input['plane_count'] = I('plane_count');
        	$input['conver'] = I('conver_area');
        	$input['plane_conver_star'] = ((int)explode(';', $input['conver'])[0])/100;
        	$input['plane_conver_end'] = ((int)explode(';', $input['conver'])[1])/100;
        	unset($input['conver']);
        	$plane = M('plane')->add($input);
            if($plane>0){
                $this->success('添加成功，正在计算，请稍后...','/Home/Shuadan/cal/id/'.$plane,0);
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->error('404 NOT FOUND');
        }
    }
    // 获取周期内计划
    public function plane_data($id){
        $data = M('shuadan')->where('plane_id="%d"',$id)->order('date asc')->select();
        for ($i=0; $i < count($data); $i++) { 
            $data[$i]['date'] = date('Y-m-d',$data[$i]['date']);
        }
        $this->ajaxReturn($data);
    }
    // 计算计划并放进数据库
    public function cal($id=null){
        $every_day = $this -> calculation($id);
        
        // $this->ajaxReturn($every_day);
        $shuadan = M('shuadan');
        for ($i=0; $i < count($every_day); $i++) { 
            $shuadan -> data($every_day[$i])->add();
        }
        $this->success('计算成功','/Home/Shuadan/index');

    }
    // 重新计算计划
    public function reload_plane($id){
        // 计划是否加锁
        $old_plane = M('plane')->where("id='%d'",$id)->find();
        if($old_plane['lock']>0){
            $this->error('该计划被锁定，不能更改！！');
        }
        $delete_result = M('shuadan')->where("plane_id='%d'",$id)->delete();//删除旧的
        // 新建新的
        $every_day = $this -> calculation($id);
        $shuadan = M('shuadan');
        for ($i=0; $i < count($every_day); $i++) { 
            $shuadan -> data($every_day[$i])->add();
        }
        for ($i=0; $i < count($every_day); $i++) { 
            $every_day[$i]['date'] = date('Y-m-d',$every_day[$i]['date']);
        }
        $this->ajaxReturn($every_day);
    }
    // 锁定计划
    public function lock_plane($id){
        $plane['lock'] = 1;
        $result = M('plane')->where("id='%d'",$id)->save($plane);
        if($result>0){
            return true;
        }
        else return false;
        // var_dump($result);
    }
    // 历史计划
    public function history(){
        $this->assign('planes',M('plane')->order('id desc')->select());
        $this->display();
    }
    // 删除计划
    public function delete($id){
        if(IS_DELETE){
        if(session('is_admin')==1){
            M('shuadan')->where("plane_id='%d'",$id)->delete();
            M('plane')->where("id='%d'",$id)->delete();
            return true;
        }else{
            $this->error('您没有该权限');
        }}else{
            $this->error('404 NOT FOUND');
        }
    }
}